{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e588e56d",
   "metadata": {},
   "outputs": [],
   "source": [
    "! pip install -q langchain_community tiktoken langchain-openai langchainhub  langchain\n",
    "! pip install -q chromadb==0.4.15\n",
    "! pip install -q beautifulsoup4\n",
    "! pip install -q langchain-nomic\n",
    "! pip install -q --upgrade httpx httpx-sse PyJWT\n",
    "! pip install -q --upgrade --quiet  dashscope"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d11276f7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()  # 加载 .env 文件中的环境变量\n",
    "\n",
    "# 获取环境变量\n",
    "def get_required_env(key):\n",
    "    value = os.getenv(key)\n",
    "    if not value:\n",
    "        raise EnvironmentError(f\"必须设置环境变量: {key}\")\n",
    "    return value\n",
    "\n",
    "\n",
    "\n",
    "# 现在可以使用 os.getenv() 获取值\n",
    "api_key = os.getenv(\"OPENAI_API_KEY\")\n",
    "\n",
    "# 从系统环境变量获取密钥\n",
    "DASHSCOPE_API_KEY = os.getenv('DASHSCOPE_API_KEY')\n",
    "DEEPSEEK_API_KEY = os.getenv('DEEPSEEK_API_KEY')\n",
    "LANGCHAIN_API_KEY = os.getenv('LANGCHAIN_API_KEY')\n",
    "OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# 验证密钥是否存在\n",
    "required_keys = {\n",
    "    'DASHSCOPE_API_KEY': DASHSCOPE_API_KEY,\n",
    "    'DEEPSEEK_API_KEY': DEEPSEEK_API_KEY,\n",
    "    'LANGCHAIN_API_KEY': LANGCHAIN_API_KEY,\n",
    "    'OPENAI_API_KEY': OPENAI_API_KEY\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dfe6764f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.embeddings import DashScopeEmbeddings\n",
    "\n",
    "missing_keys = [name for name, value in required_keys.items() if not value]\n",
    "if missing_keys:\n",
    "    raise EnvironmentError(f\"缺少环境变量: {', '.join(missing_keys)}\")\n",
    "    \n",
    "os.environ[\"LANGCHAIN_PROJECT\"] = f\"RAG_虚拟文档\"\n",
    "os.environ['LANGCHAIN_TRACING_V2'] = 'true'\n",
    "os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n",
    "os.environ['LANGCHAIN_API_KEY'] = LANGCHAIN_API_KEY\n",
    "os.environ['USER_AGENT'] = 'myagent'\n",
    "\n",
    "# 设置API密钥环境变量\n",
    "os.environ[\"DASHSCOPE_API_KEY\"] = DASHSCOPE_API_KEY\n",
    "os.environ[\"DEEPSEEK_API_KEY\"] = DEEPSEEK_API_KEY\n",
    "os.environ[\"OPENAI_API_KEY\"] = OPENAI_API_KEY\n",
    "os.environ[\"OPENAI_BASE_URL\"] = \"https://api.fe8.cn/v1\"  # 添加OpenAI基础URL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f32078f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "所有环境变量已成功设置！\n"
     ]
    }
   ],
   "source": [
    "# 现在可以安全导入其他模块\n",
    "from langchain_community.embeddings import DashScopeEmbeddings\n",
    "\n",
    "print(\"所有环境变量已成功设置！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4087fe75",
   "metadata": {},
   "outputs": [],
   "source": [
    "#### 索引 ####\n",
    "from langchain_nomic.embeddings import NomicEmbeddings\n",
    "# 加载文档\n",
    "import bs4\n",
    "from langchain_community.document_loaders import WebBaseLoader\n",
    "loader = WebBaseLoader(\n",
    "    web_paths=(\"https://baike.baidu.com/item/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/9180?fr=ge_ala#9-10\",)\n",
    ")\n",
    "blog_docs = loader.load()\n",
    "\n",
    "\n",
    "# 拆分\n",
    "from langchain.text_splitter import RecursiveCharacterTextSplitter\n",
    "text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(\n",
    "    chunk_size=300,\n",
    "    chunk_overlap=50)\n",
    "\n",
    "splits = text_splitter.split_documents(blog_docs)\n",
    "\n",
    "# 索引\n",
    "embeddings=DashScopeEmbeddings(\n",
    "    model=\"text-embedding-v1\", dashscope_api_key=DASHSCOPE_API_KEY\n",
    ")\n",
    "from langchain_community.vectorstores import Chroma\n",
    "vectorstore = Chroma.from_documents(documents=splits,\n",
    "                                    embedding=embeddings)\n",
    "\n",
    "retriever = vectorstore.as_retriever(search_kwargs={\"k\": 5})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1b63803c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好！有什么我可以帮助你的吗？\n"
     ]
    }
   ],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "\n",
    "llm=ChatOpenAI(model=\"gpt-4o\")\n",
    "# llm = ChatOpenAI(\n",
    "#     model='deepseek-reasoner',\n",
    "#     openai_api_key=DEEPSEEK_API_KEY,\n",
    "#     openai_api_base='https://api.deepseek.com'\n",
    "# )\n",
    "\n",
    "response = llm.invoke(\"你好!\")\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cdd66146",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import ChatPromptTemplate\n",
    "\n",
    "# 多重查询：不同视角\n",
    "template = \"\"\"你是一名 AI 语言模型助手。你的任务是生成给定用户问题的五个不同版本，\n",
    "以从向量数据库中检索相关文档。通过生成用户问题的多个视角，你\n",
    "的目标是帮助用户克服基于距离的相似性搜索的一些限制。\n",
    "提供这些以换行符分隔的备选问题。原始问题：{question}\"\"\"\n",
    "prompt_perspectives = ChatPromptTemplate.from_template(template)\n",
    "\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "generate_queries = (\n",
    "    prompt_perspectives\n",
    "    | llm\n",
    "    | StrOutputParser()\n",
    "    | (lambda x: x.split(\"\\n\"))\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f34fa942",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "C:\\Users\\MI\\AppData\\Local\\Temp\\ipykernel_16708\\3346451918.py:10: LangChainBetaWarning: The function `loads` is in beta. It is actively being worked on, so the API may change.\n",
      "  return [loads(doc) for doc in unique_docs]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.load import dumps, loads\n",
    "\n",
    "def get_unique_union(documents: list[list]):\n",
    "    \"\"\" 检索到的文档的唯一联合 \"\"\"\n",
    "    # 展平列表列表，并将每个文档转换为字符串\n",
    "    flattened_docs = [dumps(doc) for sublist in documents for doc in sublist]\n",
    "    # 获取独特的文档\n",
    "    unique_docs = list(set(flattened_docs))\n",
    "    # 返回\n",
    "    return [loads(doc) for doc in unique_docs]\n",
    "\n",
    "# 检索\n",
    "question = \"根据这篇百度百科链接，你能介绍一下人工智能目前在中国的现状吗？\"\n",
    "retrieval_chain = generate_queries | retriever.map() | get_unique_union\n",
    "docs = retrieval_chain.invoke({\"question\":question})\n",
    "len(docs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f7acb040",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'我无法直接访问或检索链接内容，因此无法提供该链接中的具体信息。不过，我可以分享一些关于人工智能在中国的总体情况。\\n\\n中国是全球人工智能发展的重要参与者和技术创新的领导者之一。以下是一些关键的现状概述：\\n\\n1. **政府支持**：中国政府大力支持人工智能发展，相继出台了一系列政策和战略规划。国家人工智能发展规划强调中国在人工智能技术和应用领域的领导地位。\\n\\n2. **技术创新**：中国的科技公司和研究机构在人工智能的许多领域，如自然语言处理、机器学习、计算机视觉等方面，处于国际前列。百度、阿里巴巴和腾讯等科技巨头投入大量资源开发相关技术。\\n\\n3. **应用场景**：人工智能在中国的应用非常广泛，包括智慧城市、交通管理、医疗诊断、金融服务、教育、制造业等多个领域。比如，人工智能技术在疫情期间帮助优化公共卫生管理。\\n\\n4. **投资和创业**：中国有活跃的人工智能初创生态系统，并吸引大量投资者的关注。风险投资和企业投资共同推动了该领域的快速发展。\\n\\n5. **挑战和伦理问题**：如同其他国家，中国也面临人工智能技术应用带来的伦理、隐私和安全问题。如何合理监管和使用人工智能成为重要课题。\\n\\n如果想要更详细的信息，建议查阅最新的行业报告或研究材料。'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from operator import itemgetter\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.runnables import RunnablePassthrough\n",
    "\n",
    "\n",
    "# RAG\n",
    "template = \"\"\"根据此上下文回答以下问题：\n",
    "\n",
    "{context}\n",
    "\n",
    "问题：{question}\n",
    "\"\"\"\n",
    "\n",
    "prompt = ChatPromptTemplate.from_template(template)\n",
    "\n",
    "final_rag_chain = (\n",
    "    {\"context\": retrieval_chain,\n",
    "     \"question\": itemgetter(\"question\")}\n",
    "    | prompt\n",
    "    | llm\n",
    "    | StrOutputParser()\n",
    ")\n",
    "\n",
    "final_rag_chain.invoke({\"question\":question})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "25d50339",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import ChatPromptTemplate\n",
    "\n",
    "template = \"\"\"你是一名 AI 语言模型助手。你的任务是生成给定用户问题的五个不同版本，\n",
    "以从向量数据库中检索相关文档。通过生成用户问题的多个视角，你\n",
    "的目标是帮助用户克服基于距离的相似性搜索的一些限制。\n",
    "提供这些以换行符分隔的备选问题。原始问题：{question}\"\"\"\n",
    "prompt_rag_fusion = ChatPromptTemplate.from_template(template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "db06db73",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "generate_queries = (\n",
    "    prompt_rag_fusion\n",
    "    | llm\n",
    "    | StrOutputParser()\n",
    "    | (lambda x: x.split(\"\\n\"))\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "327bf6ed",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.load import dumps, loads\n",
    "\n",
    "question = \"根据这篇百度百科链接，你能介绍一下人工智能目前在中国的现状吗？\"\n",
    "\n",
    "def reciprocal_rank_fusion(results: list[list], k=60):\n",
    "    \"\"\" Reciprocal_rank_fusion 采用多个排名文档列表\n",
    "         以及 RRF 公式中使用的可选参数 k \"\"\"\n",
    "\n",
    "    # 初始化一个字典来保存每个唯一文档的融合分数\n",
    "    fused_scores = {}\n",
    "\n",
    "    # 迭代每个排名文档列表\n",
    "    for docs in results:\n",
    "        # 迭代列表中的每个文档及其排名（列表中的位置）\n",
    "        for rank, doc in enumerate(docs):\n",
    "            # 将文档转换为字符串格式以用作键（假设文档可以序列化为 JSON）\n",
    "            doc_str = dumps(doc)\n",
    "            # 如果文档尚未在 fused_scores 字典中，则将其添加，初始分数为 0\n",
    "            if doc_str not in fused_scores:\n",
    "                fused_scores[doc_str] = 0\n",
    "            # 检索文档的当前分数（如果有）\n",
    "            previous_score = fused_scores[doc_str]\n",
    "            # 使用 RRF 公式更新文档的分数：1 / (rank + k)\n",
    "            fused_scores[doc_str] += 1 / (rank + k)\n",
    "\n",
    "    # 根据融合分数对文档进行降序排序，得到最终的重新排序结果\n",
    "    reranked_results = [\n",
    "        (loads(doc), score)\n",
    "        for doc, score in sorted(fused_scores.items(), key=lambda x: x[1], reverse=True)\n",
    "    ]\n",
    "\n",
    "    # 将重新排序的结果作为元组列表返回，每个元组包含文档及其融合分数\n",
    "    return reranked_results\n",
    "\n",
    "retrieval_chain_rag_fusion = generate_queries | retriever.map() | reciprocal_rank_fusion\n",
    "docs = retrieval_chain_rag_fusion.invoke({\"question\": question})\n",
    "len(docs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f87be8bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'根据百度百科中的信息，人工智能在中国的发展现状可以概述如下：\\n\\n1. **政策支持**：中国政府非常重视人工智能的发展，将其视为国家战略。出台了多项政策和纲要来支持和促进人工智能技术的研究和应用。这包括人工智能发展规划及专项支持政策。\\n\\n2. **技术研究与应用**：中国在人工智能的技术研究方面取得了显著的进步，特别是在机器学习、自然语言处理和计算机视觉等领域。同时，人工智能技术已广泛应用于金融、医疗、交通、教育等多个行业，提升了这些行业的效率和服务质量。\\n\\n3. **产业规模**：中国的人工智能产业规模迅速扩大，涌现了大量创新型企业和初创公司。这些公司在算法研发、数据处理以及应用解决方案方面取得了突出的成就。知名科技企业例如百度、阿里巴巴、腾讯及华为已成为人工智能领域的领导者。\\n\\n4. **人才培养**：为了支持和推动人工智能的发展，中国的高校和科研机构加快了相关人才的培养。设立专项课程和研究项目，培养了一大批人工智能领域的专业人才。\\n\\n5. **国际影响力**：凭借政策支持和技术创新，中国的人工智能在国际上取得了一定的影响力。中国的企业和研究机构正在积极参与国际合作与交流，分享研究成果，并从全球的科技潮流中获得启示。\\n\\n总的来说，中国的人工智能产业正处于快速增长阶段，未来具备良好的发展潜力和持久的创新能力。'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.runnables import RunnablePassthrough\n",
    "from operator import itemgetter\n",
    "# RAG\n",
    "template = \"\"\"根据此上下文回答以下问题：\n",
    "\n",
    "{context}\n",
    "\n",
    "问题：{question}\n",
    "\"\"\"\n",
    "\n",
    "prompt = ChatPromptTemplate.from_template(template)\n",
    "\n",
    "final_rag_chain = (\n",
    "    {\"context\": retrieval_chain_rag_fusion,\n",
    "     \"question\": itemgetter(\"question\")}\n",
    "    | prompt\n",
    "    | llm\n",
    "    | StrOutputParser()\n",
    ")\n",
    "question = \"根据这篇百度百科链接，你能介绍一下人工智能目前在中国的现状吗？\"\n",
    "final_rag_chain.invoke({\"question\":question})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "971b9630",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import ChatPromptTemplate\n",
    "\n",
    "# 分解\n",
    "template = \"\"\"您是一位乐于助人的助手，可以生成与输入问题相关的多个子问题。\\n\n",
    "目标是将输入分解为一组可以单独回答的子问题/子问题。\\n\n",
    "生成与以下问题相关的多个搜索查询：{question} \\n\n",
    "输出（5 个查询）：\"\"\"\n",
    "prompt_decomposition = ChatPromptTemplate.from_template(template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f7942d86",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "\n",
    "\n",
    "# Chain\n",
    "generate_queries_decomposition = ( prompt_decomposition | llm | StrOutputParser() | (lambda x: x.split(\"\\n\")))\n",
    "\n",
    "\n",
    "question = \"根据文章，中国的人工智能是怎么一步步发展到今天的？\"\n",
    "questions = generate_queries_decomposition.invoke({\"question\":question})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c4ea70be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1. 中国的人工智能发展历史是什么？',\n",
       " '2. 中国在人工智能领域的关键技术进步有哪些？',\n",
       " '3. 哪些政策和战略促进了中国人工智能的发展？',\n",
       " '4. 中国在人工智能研究和应用中面临的挑战有哪些？',\n",
       " '5. 中国的人工智能产业有哪些领军企业及其贡献？']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "questions"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "60e0dee8",
   "metadata": {},
   "source": [
    "## 问题分解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0639c11d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Prompt\n",
    "template = \"\"\"这是您需要回答的问题：\n",
    "\n",
    "\\n --- \\n {question} \\n --- \\n\n",
    "\n",
    "以下是任何可用的背景问题 + 答案组合：\n",
    "\n",
    "\\n --- \\n {q_a_pairs} \\n --- \\n\n",
    "\n",
    "以下是与问题相关的其他上下文：\n",
    "\n",
    "\\n --- \\n {context} \\n --- \\n\n",
    "\n",
    "使用上述上下文和任何背景问题 + 答案组合来回答问题：\\n {question}\n",
    "\"\"\"\n",
    "\n",
    "decomposition_prompt = ChatPromptTemplate.from_template(template)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "60dc9023",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n"
     ]
    }
   ],
   "source": [
    "from operator import itemgetter\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "\n",
    "def format_qa_pair(question, answer):\n",
    "    \"\"\"格式化 Q 和 A 对\"\"\"\n",
    "\n",
    "    formatted_string = \"\"\n",
    "    formatted_string += f\"Question: {question}\\nAnswer: {answer}\\n\\n\"\n",
    "    return formatted_string.strip()\n",
    "\n",
    "\n",
    "q_a_pairs = \"\"\n",
    "for q in questions:\n",
    "\n",
    "    rag_chain = (\n",
    "    {\"context\": itemgetter(\"question\") | retriever,\n",
    "     \"question\": itemgetter(\"question\"),\n",
    "     \"q_a_pairs\": itemgetter(\"q_a_pairs\")}\n",
    "    | decomposition_prompt\n",
    "    | llm\n",
    "    | StrOutputParser())\n",
    "\n",
    "    answer = rag_chain.invoke({\"question\":q,\"q_a_pairs\":q_a_pairs})\n",
    "    q_a_pair = format_qa_pair(q,answer)\n",
    "    q_a_pairs = q_a_pairs + \"\\n---\\n\"+  q_a_pair"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fbbb4ac1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'中国的人工智能产业拥有许多领军企业，它们在不同领域做出了显著贡献。以下是一些主要企业及其贡献：\\n\\n1. **百度**：\\n   - 作为中国AI领域的先锋，百度在自然语言处理、语音识别和自动驾驶技术方面处于领先地位。\\n   - 百度的Apollo计划是开放国际自动驾驶生态系统的重要举措，推动了自动驾驶技术的研发和应用。\\n\\n2. **阿里巴巴**：\\n   - 阿里巴巴将AI技术应用于电商领域，如智能物流和推荐系统，通过AI提升市场效率。\\n   - 达摩院是阿里巴巴的全球研究院，专注于高级AI技术研究，包括区块链和量子计算等。\\n\\n3. **腾讯**：\\n   - 腾讯在AI医疗影像识别、游戏和社交网络中的AI应用方面取得巨大成果。\\n   - “腾讯AI Lab”专注于计算机视觉、机器学习和自然语言处理的研究。\\n\\n4. **华为**：\\n   - 华为在AI芯片开发方面有显著贡献，其自研的昇腾系列AI芯片为智能设备提供强大的算力支持。\\n   - 华为云基于AI技术，提供诸如图像分析、语音合成等商业解决方案。\\n\\n5. **科大讯飞**：\\n   - 专注于语音识别和人工智能教育领域，其语音技术广泛应用于智能翻译、汽车智能控制等。\\n   - 讯飞在智能语音领域有着重要的创新与市场表现。\\n\\n6. **商汤科技**：\\n   - 商汤科技专注于计算机视觉技术，广泛应用于安防、金融和娱乐等多个行业。\\n   - 其算法在增强现实、自动驾驶以及智能手机的拍摄优化上有较强的应用。\\n\\n这些企业通过不断的技术创新和应用，推动了中国人工智能产业的快速发展，并在全球市场中占据重要地位。'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3834700c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一些镜头示例\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_core.prompts import ChatPromptTemplate, FewShotChatMessagePromptTemplate\n",
    "examples = [\n",
    "    {\n",
    "        \"input\": \"警察进行合法逮捕吗？\",\n",
    "        \"output\": \"警察可以做什么？\",\n",
    "    },\n",
    "    {\n",
    "        \"input\": \"Jan Sindel’s was born in what country?\",\n",
    "        \"output\": \"what is Jan Sindel’s personal history?\",\n",
    "    },\n",
    "]\n",
    "# 我们现在将它们转换为示例消息\n",
    "example_prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"human\", \"{input}\"),\n",
    "        (\"ai\", \"{output}\"),\n",
    "    ]\n",
    ")\n",
    "few_shot_prompt = FewShotChatMessagePromptTemplate(\n",
    "    example_prompt=example_prompt,\n",
    "    examples=examples,\n",
    ")\n",
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\n",
    "            \"system\",\n",
    "            \"\"\"您是世界知识方面的专家。您的任务是退一步思考，将问题解释为更通用的退一步思考问题，这样更容易回答。以下是几个示例：\"\"\",\n",
    "        ),\n",
    "        # 一些镜头示例\n",
    "        few_shot_prompt,\n",
    "        # 新的问题\n",
    "        (\"user\", \"{question}\"),\n",
    "    ]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "617372e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'人工智能可以如何分析和处理数据以帮助做出决策？'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "llm=ChatOpenAI(model=\"gpt-4o\")\n",
    "\n",
    "generate_queries_step_back = prompt | llm | StrOutputParser()\n",
    "question = \"人工智能如何用于金融风控？\"\n",
    "generate_queries_step_back.invoke({\"question\": question})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c3235d3a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n",
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'AI在中国的发展历程可以追溯到20世纪50年代。以下是中国人工智能发展的几个重要阶段：\\n\\n1. **早期探索（1950s-1970s）**: 中国的AI研究始于1950年代末。当时，中国的科技工作者开始探索计算机应用，并逐步涉足人工智能的基础研究。虽然技术和资源非常有限，但这为后续的发展奠定了基础。\\n\\n2. **基础设施建设（1980s）**: 到1980年代，中国政府开始注重信息技术和计算机科学的发展。中国科学院和其他科研机构成立了专门的研究部门，以推动人工智能技术以及相关计算领域的进步。\\n\\n3. **研究和应用持续发展（1990s）**: 在90年代，中国的AI研究进入了快速发展阶段。随着计算机技术的进步，AI在自然语言处理、专家系统和机器翻译等领域取得了实质性进展。国家科技政策支持人工智能和计算机科学的研发，推动了科研成果的实际应用。\\n\\n4. **全球参与和本土创新（2000s-2010s）**: 随着互联网的普及和全球技术交流的增多，中国的AI产业开始与国际接轨。在这期间，中国涌现了一批具有国际影响力的科技公司，比如百度、腾讯和阿里巴巴，这些公司在AI研究和商业化上都表现出了较强的竞争力。中国开始在AI的多个领域，包括语音识别、图像处理和自动驾驶等方面取得领先地位。\\n\\n5. **新时代的快速发展（2010s-至今）**: 进入21世纪第二个十年后，中国的AI发展进入了黄金时期。中国政府制定了多项政策和战略以支持AI产业的发展，如《中国制造2025》和《新一代人工智能发展规划》。这一时期，中国在AI技术的研究、创新和应用方面取得了显著的进步，同时在AI伦理和政策制定上也开始有深入讨论。AI技术被广泛应用于金融、医疗、交通和教育等领域。\\n\\n这种发展历程展示了中国在国际舞台上日益增强的科技竞争力和影响力。'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.runnables import RunnableLambda\n",
    "# 响应提示\n",
    "response_prompt_template = \"\"\"您是世界知识专家。我要问您一个问题。您的回答应全面，并且不与以下上下文相矛盾（如果它们相关）。否则，如果它们不相关，请忽略它们。\n",
    "\n",
    "# {normal_context}\n",
    "# {step_back_context}\n",
    "\n",
    "# 原始问题：{question}\n",
    "# 答案：\"\"\"\n",
    "response_prompt = ChatPromptTemplate.from_template(response_prompt_template)\n",
    "\n",
    "chain = (\n",
    "    {\n",
    "        # 使用普通问题检索上下文\n",
    "        \"normal_context\": RunnableLambda(lambda x: x[\"question\"]) | retriever,\n",
    "        # 使用后退问题检索上下文\n",
    "        \"step_back_context\": generate_queries_step_back | retriever,\n",
    "        # 把问题转过去\n",
    "        \"question\": lambda x: x[\"question\"],\n",
    "    }\n",
    "    | response_prompt\n",
    "    | llm\n",
    "    | StrOutputParser()\n",
    ")\n",
    "\n",
    "chain.invoke({\"question\": question})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "21da7063",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'人工智能（AI）在中国的发展历程是一段充满活力和创新的历史。从早期起步到如今的国际领先，中国的AI研究和应用已经经历了几个重要阶段。\\n\\n20世纪50年代，中国的学者开始关注人工智能的理论研究，当时主要受到了海外特别是美国的研究影响。经过数十年的发展，中国在逻辑推理、自然语言处理等领域已经形成了一定基础。然而，由于种种原因，这一时期的进展较为缓慢，更多的是理论层面的探索。\\n\\n进入21世纪，随着计算能力的提升以及数据资源的丰富，中国的AI研究进入了新的发展阶段。在这一时期，中国政府和企业投入了大量资源，用于推动技术革新和应用研究。尤其是2010年之后，得益于深度学习算法的突破和大数据技术的助力，中国的AI技术取得了飞跃性的发展。\\n\\n2017年，中国政府发布了《新一代人工智能发展规划》，明确指出要将AI作为国家战略。这一规划为人工智能的发展注入了政策支持和资金资源，促使中国在AI技术的竞赛中加速前进。政府、企业和科研机构之间的协作显著提升了AI研发的效率和成果转化速度。\\n\\n如今，中国已成为人工智能领域的全球领导者之一，在实际应用如智能交通、智慧城市、医疗健康、无人驾驶等方面取得了显著成效。中国企业如百度、阿里巴巴、腾讯、华为等不仅在国内市场表现突出，也积极参与国际竞争。中国的高校和研究机构也在国际刊物上发表了众多有影响力的研究成果。\\n\\n为了维护这项优势，中国持续推动AI在教育、国家安全、基础设施等领域的应用，同时加强伦理研究，确保技术的发展与社会利益相协调。随着技术的不断进步和市场需求的增长，中国的人工智能正朝着更加广泛和深入的方向发展，在全球科技舞台上扮演着愈发重要的角色。'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain.prompts import ChatPromptTemplate\n",
    "\n",
    "# 虚拟文档生成\n",
    "template = \"\"\"请写一段介绍性文章来回答问题\n",
    "问题：{question}\n",
    "文章：\"\"\"\n",
    "prompt_hyde = ChatPromptTemplate.from_template(template)\n",
    "\n",
    "from langchain_core.output_parsers import StrOutputParser\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "generate_docs_for_retrieval = (\n",
    "    prompt_hyde | llm | StrOutputParser()\n",
    ")\n",
    "\n",
    "question = \"AI在中国的发展历程\"\n",
    "generate_docs_for_retrieval.invoke({\"question\":question})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9d482559",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Number of requested results 5 is greater than number of elements in index 1, updating n_results = 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[Document(metadata={'language': 'en', 'source': 'https://baike.baidu.com/item/%E4%BA%BA%E5%B7%A5%E6%99%BA%E8%83%BD/9180?fr=ge_ala#9-10', 'title': '百度安全验证'}, page_content='百度安全验证')]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检索\n",
    "retrieval_chain = generate_docs_for_retrieval | retriever\n",
    "retireved_docs = retrieval_chain.invoke({\"question\":question})\n",
    "retireved_docs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "affbc284",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'AI在中国的发展历程可以追溯到20世纪50年代。当时，中国的科学家开始关注人工智能领域，并展开了一些初步研究。随后，随着计算机技术和互联网的快速发展，中国的AI研究逐渐扩大。\\n\\n80年代，中国的AI研究进入了一个新的阶段，特别是在自然语言处理和机器翻译领域取得了显著的进展。90年代，随着全球信息技术的迅猛发展，中国的AI研究逐步接轨国际前沿，学术界与产业界都积极参与AI技术的开发。\\n\\n进入21世纪，尤其是2006年以后，中国政府开始大力支持AI的发展。政府实施了多项政策，促进科研和产业融合，推动AI技术的创新。近年来，中国涌现出许多AI企业，专注于计算机视觉、语音识别、智能机器人等领域，并取得了显著成绩。\\n\\n此外，中国的互联网公司如百度、阿里巴巴、腾讯等，在AI技术研发方面加大投入，并且在自动驾驶、智能客服、数据分析等方面展现了强大的技术实力。\\n\\n近年来，中国政府还出台了一系列政策来支持AI的发展，例如“新一代人工智能发展规划”，进一步推动AI技术应用和产业化。总体来看，中国的AI产业正在以迅猛的速度向前发展，成为全球重要的AI技术研发和应用中心之一。'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# RAG\n",
    "template = \"\"\"根据此上下文回答以下问题：\n",
    "\n",
    "{context}\n",
    "\n",
    "问题：{question}\n",
    "\"\"\"\n",
    "\n",
    "prompt = ChatPromptTemplate.from_template(template)\n",
    "\n",
    "final_rag_chain = (\n",
    "    prompt\n",
    "    | llm\n",
    "    | StrOutputParser()\n",
    ")\n",
    "\n",
    "final_rag_chain.invoke({\"context\":retireved_docs,\"question\":question})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8c920ba",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10",
   "language": "python",
   "name": "python310"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
